#!/usr/bin/env python3
"""
简化的Text2SQL评估系统启动脚本
"""
import os
import sys
import subprocess
import webbrowser
import time
import threading

def start_backend():
    """启动后端服务"""
    print("启动后端服务...")
    os.chdir("backend")
    try:
        subprocess.run([sys.executable, "app.py"])
    except KeyboardInterrupt:
        print("\n后端服务已停止")

def start_frontend():
    """启动前端服务"""
    print("启动前端服务...")
    os.chdir("frontend")
    try:
        subprocess.run([sys.executable, "-m", "http.server", "8080"])
    except KeyboardInterrupt:
        print("\n前端服务已停止")

def main():
    """主函数"""
    print("=== Text2SQL评估系统 ===")
    print("正在启动服务...")
    
    # 检查环境变量文件
    if not os.path.exists("backend/.env"):
        print("⚠ 未找到 backend/.env 文件")
        print("请创建 backend/.env 文件并设置 OPENAI_API_KEY")
        print("参考 backend/env_example.txt 文件")
        return
    
    # 启动后端服务（在后台线程中）
    backend_thread = threading.Thread(target=start_backend)
    backend_thread.daemon = True
    backend_thread.start()
    
    # 等待后端启动
    print("等待后端服务启动...")
    time.sleep(3)
    
    # 启动前端服务
    try:
        start_frontend()
    except KeyboardInterrupt:
        print("\n服务已停止")

if __name__ == "__main__":
    main()
